<!-- 堆积柱状图 -->
<template>
  <div ref="chartContainer" style="width: 348px; height: 104px"></div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, onUpdated } from "vue";
import * as echarts from "echarts";

// 接收外部传入的数据
const props = defineProps({
  categories: {
    type: Array,
    required: true,
    default: () => ["空间类", "公共服务类", "工程项目类", "个人信息类", "管理类", "指标类", "业务类"],
  },
  digitizedCounts: {
    type: Array,
    required: true,
    default: () => [100, 200, 30, 110, 100, 120, 120],
  },
  undigitizedCounts: {
    type: Array,
    required: true,
    default: () => [100, 200, 30, 110, 100, 120, 120],
  },
  totalCount: {
    type: Number,
    required: true,
    default: () => 1000,
  },
  digitizationRate: {
    type: Number,
    required: true,
    default: () => 80,
  },
});

const chartContainer = ref(null);
let chartInstance = null;

const initChart = () => {
  if (chartContainer.value) {
    chartInstance = echarts.init(chartContainer.value);
    const option = {
      backgroundColor: "transparent",
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
        // formatter: function (params) {
        //   return params[0].name;
        // },
      },
      legend: {
        data: ["数据资源数"],
        right: 0,
        textStyle: {
          color: "#fff",
        },
      },
      grid: {
        left: "15%",
        right: "4%",
        top: "35%",
        bottom: "5%",
        containLabel: false,
      },
      xAxis: {
        type: "category",
        data: props.categories,
        axisLine: {
          lineStyle: {
            color: "#d2e0f4",
          },
        },
        axisTick: {
          show: false,
        },
        axisLabel: {
          show: false,
          color: "#fff",
          rotate: 30,
        },
      },
      yAxis: {
        type: "value",
        axisLine: {
          lineStyle: {
            color: "#fff",
          },
        },
        axisTick: {
          show: false,
        },
        axisLabel: {
          color: "#fff",
          margin: 16,
          fontSize: 12,
          interval: "auto",
          formatter: function (value) {
            if (value >= 1000) {
              return value / 1000 + "k";
            }
            return value;
          },
        },
        splitLine: {
          lineStyle: {
            color: "#333",
          },
        },
      },
      series: [
        {
          name: "数据资源数",
          type: "bar",
          stack: "总量",
          barWidth: "20%",
          itemStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#3C7DFB",
                },
                {
                  offset: 1,
                  color: "#0A285F",
                },
              ]),
            },
          },

          data: props.digitizedCounts,
        },
      ],
      title: [
        {
          text: `数据资源已分类总数: ${props.totalCount} 个`,
          left: "left",
          top: "0",
          textStyle: {
            color: "#fff",
            fontSize: 12,
          },
        },
        {
          // text: `单位:个`,
          right: "130",
          top: "0",
          textStyle: {
            color: "#fff",
            fontSize: 12,
          },
        },
      ],
    };
    chartInstance.setOption(option);
  }
};

onMounted(() => {
  console.log(props.totalCount, "666666");

  initChart();
});

onUnmounted(() => {
  if (chartInstance) {
    chartInstance.dispose();
    chartInstance = null;
  }
});
</script>

<style scoped>
/* 可添加额外样式 */
</style>
